package com.dragon.app.controller.auth;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.app.config.annotation.Login;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.logical.service.auth.IExamineService;
import com.dragon.logical.service.auth.PositionService;
import com.dragon.repository.domain.dto.ExamineDTO;
import com.dragon.repository.domain.vo.ExamineUserVO;
import com.dragon.repository.domain.vo.ExamineVO;
import com.dragon.repository.entity.Admin;
import com.dragon.repository.entity.Examine;
import com.dragon.repository.entity.FromDetail;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * app 审批
 * @author Charlotte
 */
@RestController
@RequestMapping("examine")
@Api(tags = "审批")
public class ExamineController {

    @Autowired
    IExamineService service;

    @Autowired
    PositionService positionService;


    @GetMapping("list")
    @ApiOperation(value = "获取申请或者审批详情")
    public ResponseData selectDetailsById(@RequestParam Integer id,@RequestParam Integer type){
        FromDetail list = service.selectDetailsById(id,type);
        return ResponseData.success(list);
    }

    @GetMapping("getList")
    @ApiOperation(value = "获取审批详细list")
    public ResponseData getDetailsList(@RequestParam Integer id, @RequestParam Integer type){
        List<Examine> detailsList = service.getDetailsList(id, type);
        return ResponseData.success(detailsList);
    }

    @GetMapping("applicantPageList")
    @ApiOperation(value = "申请分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status",required = false,type = "int",value = "状态"),
            @ApiImplicitParam(name = "type",required = false,type = "int",value = "流程类型"),
            @ApiImplicitParam(name = "page",required = true,type = "int",value = "页码"),
            @ApiImplicitParam(name = "limit",required = true,type = "int",value = "条数")
    })
    public ResponseData<List<ExamineDTO>> selectApplicantPageList(
            @RequestParam(required = false) Integer status, @RequestParam(required = false) Integer type,
            @ApiParam(hidden = true) @Login Integer applicantId, @RequestParam Integer page, @RequestParam Integer limit){
        Page<Examine> pageResult = service.selectApplicantPageList(status,type,applicantId,page,limit);
        return ResponseData.success(ExamineDTO.convertList(pageResult.getRecords()), pageResult.getTotal());
    }

    @GetMapping("auditPageList")
    @ApiOperation(value = "审批分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status",required = false,type = "int",value = "状态"),
            @ApiImplicitParam(name = "type",required = false,type = "int",value = "流程类型"),
            @ApiImplicitParam(name = "page",required = true,type = "int",value = "页码"),
            @ApiImplicitParam(name = "limit",required = true,type = "int",value = "条数")
    })
    public ResponseData<List<ExamineDTO>> selectAuditPageList(
            @RequestParam(required = false) Integer status,@RequestParam(required = false) Integer type,
            @ApiParam(hidden = true) @Login Integer auditId, @RequestParam Integer page, @RequestParam Integer limit
    ){
        Page<Examine> pageResult = service.selectAuditPageList(status,type,auditId,page,limit);
        return ResponseData.success(ExamineDTO.convertList(pageResult.getRecords()), pageResult.getTotal());
    }

//    @PostMapping("audit")
//    @ApiOperation(value = "审核操作")
//    public ResponseData auditOperation(@RequestBody ExamineVO vo){
//        service.auditOperation(ExamineVO.convert(vo));
//        return ResponseData.success();
//    }

    @GetMapping("oneLevel")
    @ApiOperation("选择主管")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "grade", value = "级别", required = false, dataType = "int"),
            @ApiImplicitParam(name = "departmentId", value = "部门id", required = false, type = "string")
    })
    public ResponseData oneLevel( @RequestParam(required = false) Integer grade , @RequestParam(required = false) String departmentId ){
        List<Admin> admins = positionService.levelPermissions(grade,departmentId);
        return ResponseData.success(admins);
    }

    @PostMapping("addSign")
    @ApiOperation(value = "添加加签")
    public ResponseData addSign(@RequestBody @Validated(Insert.class) ExamineVO vo){

        service.addSign(ExamineVO.convert(vo));
        return ResponseData.success();
    }

    @GetMapping("isAudit")
    @ApiOperation(value = "检测当前用户是否有审核的权限")
    public ResponseData<ExamineDTO> getUserIsAuditBy(
            @ApiParam(hidden = true) @Login Integer adminId,
            @ApiParam(name = "type",required = true,type = "int",value = "流程类型id") @RequestParam Integer type,
            @ApiParam(name = "mainId",required = true,type = "int",value = "主表id") @RequestParam Integer mainId){
        Examine userIsAuditBy = service.getUserIsAuditBy(adminId, type, mainId);
        return ResponseData.success( ExamineDTO.convert(userIsAuditBy));
    }

    @PostMapping("audit")
    @ApiOperation(value = "审核")
    public  ResponseData audit(@RequestBody @Validated(Insert.class) ExamineUserVO vo){
        service.audit(ExamineUserVO.convert(vo));
        return ResponseData.success();
    }


}
